Skip to content

Return null from GsonHelper array getters for a non-array property#4056

Merged
binarywang merged 1 commit into
binarywang:developfrom
vasiliy-mikhailov:fix/gsonhelper-non-array-graceful
Jul 10, 2026
Merged

Return null from GsonHelper array getters for a non-array property#4056
binarywang merged 1 commit into
binarywang:developfrom
vasiliy-mikhailov:fix/gsonhelper-non-array-graceful

Conversation

@vasiliy-mikhailov

@vasiliy-mikhailov vasiliy-mikhailov commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

GsonHelper.getIntArray, getStringArray and getLongArray call gson's JsonObject.getAsJsonArray(memberName), which casts the member to JsonArray. If the property exists but is not a JSON array (e.g. a string or number), this throws an unchecked ClassCastException instead of returning gracefully.

This passes the raw element via o.get(string) to the existing null-safe getAsJsonArray(JsonElement) helper, and makes that helper treat a non-array element the same as null — so the three methods return null for a non-array property, matching how they already handle a missing/null property. The other caller of the helper (WxOpenAuthorizationInfoGsonAdapter) is unaffected for valid arrays and becomes null-safe for malformed input.

Adds regression tests for the three methods that fail before the change (CCE) and pass after.

AI assistance disclosure

This contribution was produced with the help of an AI pipeline. The pipeline processed a large amount of source code to surface suspected bugs, reproduced a subset of them with failing unit tests and generated candidate fixes, and prepared pull requests from the ones that held up. Each PR was then reviewed and verified by a human before being opened: the fix and test were checked by hand and the test was confirmed to fail before the change and pass after.

…ray property

These methods call gson JsonObject.getAsJsonArray(memberName), which casts the member to JsonArray and throws ClassCastException when the property exists but is not an array (e.g. a string or number). Pass the raw element to the null-safe getAsJsonArray(JsonElement) helper and make that helper treat a non-array element like null, so the methods return null for a non-array property just as they already do for a missing one. Adds regression tests.
@binarywang

Copy link
Copy Markdown
Owner

@codex auggie review

@augmentcode

augmentcode Bot commented Jun 25, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: Makes GsonHelper’s array getters return null (instead of throwing) when a JSON member exists but is not an array.

Changes:

  • Switch getIntArray/getStringArray/getLongArray to read via JsonObject.get() and the shared getAsJsonArray(JsonElement) helper.
  • Harden getAsJsonArray to return null for null/JsonNull/non-array elements to avoid unchecked ClassCastException.
  • Add regression tests ensuring the three getters are null-safe for non-array properties.
Notes: Improves robustness against malformed input while keeping behavior unchanged for valid arrays.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

@chatgpt-codex-connector

Copy link
Copy Markdown

Review 结论

未发现需要阻塞合并的问题。该 PR 将 getIntArraygetStringArraygetLongArray 从直接调用 JsonObject#getAsJsonArray(String) 改为先取 JsonElement 再走统一的 getAsJsonArray(JsonElement),避免非数组字段触发 ClassCastException;同时 getAsJsonArray 对非数组元素返回 null,与缺失/空字段的处理语义一致。相关实现集中在 GsonHelper 的数组 getter 和 helper 方法中。 weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.javaL110-L153

新增的回归用例覆盖了三个数组 getter 遇到非数组属性时返回 null 的行为,能有效防止该问题回归。 weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/GsonHelperTest.javaL140-L163

重点检查项

Testing

  • git diff --find-renames 1697535faeedb3509d202d9f6e6ccbaa29323de5...780000b0a3e777a1c806a19c054a2092779e1d4c -- weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/GsonHelperTest.java
  • rg "getAsJsonArray\\(" -n weixin-java-common/src/main/java weixin-java-common/src/test/java | head -50
  • ⚠️ mvn -pl weixin-java-common -Dtest=me.chanjar.weixin.common.util.json.GsonHelperTest test(环境访问 Maven Central 返回 403,导致依赖/插件无法解析,测试未能实际执行)

View task →

@binarywang binarywang added this to the 4.8.5 milestone Jun 26, 2026
@binarywang binarywang requested a review from Copilot July 10, 2026 14:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 修复了 GsonHelper 在读取数组属性时的健壮性问题:当 JSON 属性存在但类型不是数组时,不再抛出未检查的 ClassCastException,而是与“属性缺失/为 null”保持一致地返回 null。这属于 weixin-java-common 通用 JSON 工具层的容错增强,可降低上层反序列化在面对畸形输入时的崩溃风险。

Changes:

  • getIntArray / getStringArray / getLongArray 改为先取原始 JsonElemento.get(name)),再走统一的 getAsJsonArray(JsonElement) 判定逻辑,避免 Gson 的强制类型转换导致的 CCE。
  • getAsJsonArray(JsonElement) 在元素非数组(含 JsonNull、primitive、object 等)时返回 null,与 “null element” 行为对齐。
  • 新增回归测试:覆盖三种数组 getter 在“属性存在但非数组”场景下应返回 null 且不抛异常。

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java 统一数组读取入口,避免非数组属性触发 ClassCastException,提升畸形输入容错性。
weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/GsonHelperTest.java 增加三项回归测试,验证非数组属性时数组 getter 返回 null

@binarywang binarywang merged commit 6ca6c34 into binarywang:develop Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants